<cfcomponent output="false">
	
	<!--- --->

	<cffunction name="getGoals" access="public" output="true" returntype="query">
		<cfargument name="userID" required="false" default="#request.session.user.id#" />
		<cfargument name="goalID" required="false" />
		<cfargument name="startdate" required="false" />
		<cfargument name="enddate" required="false" />
		<cfargument name="time_frame" required="false" default="" />
		
		<cfset var local = {} />

		<cfif structKeyExists(arguments,"startdate")>
			<cfset arguments.startdate = $.date.localToUTC(arguments.startdate) />
		</cfif>

		<cfif structKeyExists(arguments,"enddate")>
			<cfset arguments.enddate = $.date.localToUTC(arguments.enddate) />
		</cfif>

		<cfquery name="local.getGoals" datasource="#$.site.getVar('datasource')#">
			select 
			hex(goal.id) as id,
			goal.name,
			goal.description,
			date_format(goal.start,'%m/%d/%Y') as start,
			date_format(goal.stop,'%m/%d/%Y') as stop,
			goal.start as goal_start,
			goal.stop as goal_stop,
			goal.measurable,
			goal.unit,
			goal.abandoned,
			goal.created,
			ifNull(action_count.count,0) as action_count,
			ifNull(milestone_count.count,0) as milestone_count,
			target_milestone.target_count
			from goal
			left join (
				select
				count(goal_action.id) as count,
				goal_action.goal_id
				from goal_action
				where goal_action.active = 1
				group by goal_action.goal_id
			) action_count on action_count.goal_id = goal.id
			left join (
				select
				count(goal_milestone.id) as count,
				goal_milestone.goal_id
				from goal_milestone
				where goal_milestone.active = 1
				group by goal_milestone.goal_id
			) milestone_count on milestone_count.goal_id = goal.id
			left join goal_milestone target_milestone on target_milestone.goal_id = goal.id
				and target_milestone.active = 1
				and target_milestone.locked = 1
			where goal.active = 1
			and goal.user_id = #$.database.uuid(arguments.userID)#
			<cfif structKeyExists(arguments,"goalID")>
				and goal.id = #$.database.uuid(arguments.goalID)#
			</cfif>
			<cfif structKeyExists(arguments,"startdate")>
				and goal.start >= #$.database.datetime(arguments.startdate)#
			</cfif>
			<cfif structKeyExists(arguments,"enddate")>
				and goal.start < #$.database.datetime(arguments.enddate)#
			</cfif>
			<cfif arguments.time_frame eq "past">
				and date_add(goal.stop,INTERVAL 1 DAY) < #$.database.datetime(now())#
			</cfif>
			<cfif arguments.time_frame eq "present">
				and date_add(goal.stop,INTERVAL 1 DAY) >= #$.database.datetime(now())#
				and goal.start <= #$.database.datetime(now())#
			</cfif>
			<cfif arguments.time_frame eq "future">
				and goal.start > #$.database.datetime(now())#
			</cfif>
			
			order by goal.created desc
		</cfquery>

		<cfreturn local.getGoals />
		
	</cffunction>
	
	<!--- --->

	<cffunction name="getMilestones" access="public" output="true" returntype="query">
		<cfargument name="goalID" required="false" default="" />
		<cfargument name="milestoneID" required="false" />
		<cfargument name="startdate" required="false" />
		<cfargument name="enddate" required="false" />
		<cfargument name="limit" required="false" />
		<cfargument name="locked" required="false" default="" />
		<cfargument name="complete" required="false" default="" />
		<cfargument name="order" required="false" default="goal_milestone.date asc" />
		<cfargument name="goalAbandoned" required="false" />
		
		<cfset var local = {} />

		<cfif structKeyExists(arguments,"startdate")>
			<cfset arguments.startdate = $.date.localToUTC(arguments.startdate) />
		</cfif>

		<cfif structKeyExists(arguments,"enddate")>
			<cfset arguments.enddate = $.date.localToUTC(arguments.enddate) />
		</cfif>

		<cfquery name="local.getMilestones" datasource="#$.site.getVar('datasource')#">
			select 
			hex(goal_milestone.id) as id,
			goal_milestone.description,
			goal_milestone.date as date,
			hex(goal_milestone.goal_id) as goal_id,
			goal_milestone.target_count,
			goal_milestone.actual_count,
			goal_milestone.complete,
			goal_milestone.locked,
			goal.name as goal_name,
			goal.measurable as goal_measurable,
			goal.unit as goal_unit
			from goal_milestone
			inner join goal on goal.id = goal_milestone.goal_id
			where goal_milestone.active = 1
			and goal.user_id = #$.database.uuid(request.session.user.id)#
			<cfif arguments.goalID neq "">
				and goal_milestone.goal_id = #$.database.uuid(arguments.goalID)#
			</cfif>
			<cfif structKeyExists(arguments,"milestoneID")>
				and goal_milestone.id = #$.database.uuid(arguments.milestoneID)#
			</cfif>
			<cfif structKeyExists(arguments,"startdate")>
				and goal_milestone.date >= #$.database.datetime(arguments.startdate)#
			</cfif>
			<cfif structKeyExists(arguments,"enddate")>
				and goal_milestone.date < #$.database.datetime(arguments.enddate)#
			</cfif>
			<cfif arguments.locked neq "">
				and goal_milestone.locked in #$.database.strings(arguments.locked)#
			</cfif>
			<cfif arguments.complete neq "">
				and goal_milestone.complete = #$.database.string(arguments.complete)#
			</cfif>
			<cfif structKeyExists(arguments,"goalAbandoned")>
				and goal.abandoned = #$.database.string(arguments.goalAbandoned)#
			</cfif>
			order by #arguments.order#
			<cfif structKeyExists(arguments,"limit")>
				limit #arguments.limit#
			</cfif>
		</cfquery>
		
		<cfreturn local.getMilestones />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="getActions" access="public" output="true" returntype="query">
		<cfargument name="goalID" required="true" />
		<cfargument name="actionID" required="false" />
		<cfargument name="milestoneID" required="false" />
		<cfargument name="limit" required="false" />
		
		<cfset var local = {} />

		<cfquery name="local.getActions" datasource="#$.site.getVar('datasource')#">
			select 
			hex(goal_action.id) as id,
			hex(goal_action.goal_milestone_id) as goal_milestone_id,
			goal_action.name,
			goal_milestone.description as milestone_description
			from goal_action
			left join goal_milestone on goal_milestone.id = goal_action.goal_milestone_id
			where goal_action.active = 1
			and goal_action.goal_id = #$.database.uuid(arguments.goalID)#
			<cfif structKeyExists(arguments,"actionID")>
				and goal_action.id = #$.database.uuid(arguments.actionID)#
			</cfif>
			<cfif structKeyExists(arguments,"milestoneID")>
				and goal_action.goal_milestone_id = #$.database.uuid(arguments.milestoneID)#
			</cfif>
			order by goal_action.created asc
			<cfif structKeyExists(arguments,"limit")>
				limit #arguments.limit#
			</cfif>
		</cfquery>
		
		<cfreturn local.getActions />
		
	</cffunction>
	
	<!--- --->

	<cffunction name="bulkDeleteMilestones" access="public" output="true" returntype="void">
		<cfargument name="goalID" required="false" default="" />
		<cfargument name="milestoneIDs" required="false"default="" />
		
		<cfset var local = {} />

		<cfif arguments.goalID neq "" or arguments.milestoneIDs neq "">
			<cfquery name="local.bulkDeleteMilestones" datasource="#$.site.getVar('datasource')#">
				update goal_milestone
				set active = 0,
					modified = now()
				where goal_milestone.active = 1
				<cfif arguments.goalID neq "">
					and goal_milestone.goal_id = #$.database.uuid(arguments.goalID)#
				</cfif>
				<cfif arguments.milestoneIDs neq "">
					and goal_milestone.id in #$.database.uuids(arguments.milestoneIDs)#
				</cfif>
			</cfquery>
		</cfif>
		
	</cffunction>
	
	<!--- --->

	<cffunction name="bulkDeleteActions" access="public" output="true" returntype="void">
		<cfargument name="goalID" required="false" default="" />
		<cfargument name="actionIDs" required="false"default="" />
		
		<cfset var local = {} />

		<cfif arguments.goalID neq "" or arguments.actionIDs neq "">
			<cfquery name="local.bulkDeleteActions" datasource="#$.site.getVar('datasource')#">
				update goal_action
				set active = 0,
					modified = now()
				where goal_action.active = 1
				<cfif arguments.goalID neq "">
					and goal_action.goal_id = #$.database.uuid(arguments.goalID)#
				</cfif>
				<cfif arguments.actionIDs neq "">
					and goal_action.id in #$.database.uuids(arguments.actionIDs)#
				</cfif>
			</cfquery>
		</cfif>
		
	</cffunction>
	
	<!--- --->

	<!--- <cffunction name="getUpcomingMilestones" access="public" output="true" returntype="query">
		<cfargument name="startdate" required="false" default="#$.date.getDate()#" />
		<cfargument name="enddate" required="false" default="#dateAdd('d', 45, $.date.getDate())#" />
		<cfargument name="userID" required="false" default="#request.session.user.id#" />

		<cfset var local = {} />

		<cfset arguments.startdate = $.date.localToUTC(arguments.startDate) />
		<cfset arguments.enddate = $.date.localToUTC(arguments.endDate) />

		<cfquery name="local.getUpcomingMilestones" datasource="#$.site.getVar('datasource')#">
			select
			hex(goal_milestone.id) as id,
			goal_milestone.description,
			goal_milestone.date,
			hex(goal_milestone.goal_id) as goal_id,
			goal_milestone.complete,
			goal_milestone.locked,
			goal.name as goal_name
			from goal_milestone
			inner join goal on goal.id = goal_milestone.goal_id and goal.active = 1
			where goal_milestone.active = 1
			and goal.user_id = #$.database.uuid(arguments.userID)#
			and goal_milestone.date >= #$.database.datetime(arguments.startdate)#
			and goal_milestone.date < addDate(#$.database.datetime(arguments.enddate)#,1)
			order by goal_milestone.date asc
		</cfquery>

		<cfreturn local.getUpcomingMilestones />

	</cffunction> --->

	<!--- --->

</cfcomponent>